package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.CropsBean;

import java.util.ArrayList;

/**
 * 农作物类型数据适配
 */
public class CpTypeadapter extends BaseAdapter {

    private Context context;
    //    private String[] typeStr = {"叶根类", "根茎类", "茄果类", "白菜类", "豆类", "薯芋类", "花菜类", "食用菌", "瓜类", "其他类"};
    private ArrayList<Boolean> arrayListBoolean;

    public ArrayList<Boolean> getArrayListBoolean() {
        return arrayListBoolean;
    }

    private CropsBean cropsBean;
    private String title;

    public void setArrayListBoolean(ArrayList<Boolean> arrayListBoolean) {
        this.arrayListBoolean = arrayListBoolean;
    }

    public CpTypeadapter(Context context, CropsBean cropsBean) {
        this.context = context;
        this.cropsBean = cropsBean;
    }

    public CpTypeadapter(Context context, CropsBean cropsBean, String title) {
        this.context = context;
        this.title = title;
        this.cropsBean = cropsBean;
    }

    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public int getCount() {
        if (title != null) {
            return cropsBean.getV().getALLCROPS().size() + 1;
        }
        return cropsBean.getV().getALLCROPS().size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cp_type, parent, false);
            holder = new ViewHolder();
            holder.tv_cp_type = (TextView) convertView.findViewById(R.id.tv_cp_type);
            holder.tv_cp_add = (TextView) convertView.findViewById(R.id.tv_cp_add);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (title != null) {
            holder.tv_cp_type.setVisibility(View.GONE);
            holder.tv_cp_add.setVisibility(View.GONE);
            if(position==cropsBean.getV().getALLCROPS().size()){
                holder.tv_cp_add.setVisibility(View.VISIBLE);
            }else{
                holder.tv_cp_type.setVisibility(View.VISIBLE);
                if (getArrayListBoolean().get(position)){
                    holder.tv_cp_type.setBackgroundResource(R.mipmap.imgbg);
                } else {
                    holder.tv_cp_type.setBackgroundResource(R.color.mp_bround);
                }
                holder.tv_cp_type.setText(cropsBean.getV().getALLCROPS().get(position).getCropTypeName());
            }
        } else {
            holder.tv_cp_type.setVisibility(View.VISIBLE);
            holder.tv_cp_add.setVisibility(View.GONE);
            if (getArrayListBoolean().get(position)) {
                holder.tv_cp_type.setBackgroundResource(R.mipmap.imgbg);
            } else {
                holder.tv_cp_type.setBackgroundResource(R.color.mp_bround);
            }
            holder.tv_cp_type.setText(cropsBean.getV().getALLCROPS().get(position).getCropTypeName());
        }
        holder.tv_cp_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = handler.obtainMessage();
                msg.what = v.getId();
                msg.arg1 = position;
                msg.obj = cropsBean.getV().getALLCROPS().get(position).getCropTypeName();
                handler.sendMessage(msg);
            }
        });
        holder.tv_cp_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = handler.obtainMessage();
                msg.what = v.getId();
                msg.arg1 = position;
                handler.sendMessage(msg);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView tv_cp_type;
        TextView tv_cp_add;
    }
}
